package s02;

import java.io.*;
import java.net.Socket;

/**
 * @ClassName FileUploadClient
 * @Description TODO
 * @Author Li
 * @Date 2022/9/9 0:58
 * @ModifyDate 2022/9/9 0:58
 * @Version 1.0
 */
public class FileUploadClient {
    public static void main(String[] args) throws IOException {
        // 1.创建流对象
        String filename = System.getProperty("user.dir") + File.separator + "src" + File.separator + "s01" + File.separator + "lol.png";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
        // 2.创建输出流，写到服务器
        Socket socket = new Socket("127.0.0.1", 6666);
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        // 3.写数据
        byte[] bytes = new byte[10240];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }
        // 4.关闭输出流，通知客户端，写数据完毕
        socket.shutdownOutput();
        System.out.println("文件发送完毕！");
        // 5.解析回写
        InputStream is = socket.getInputStream();
        byte[] back = new byte[1024];
        is.read(back);
        System.out.println(new String(back));
        is.close();
        // 6.释放资源
        socket.close();
        bis.close();
    }
}
